<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of CategoryController
 *
 * @author Hoang Thanh Duc
 */
class Admin_CategoryController extends Zend_Controller_Action {
    function indexAction() {
        $categoryDao = new CategoryDAO();
        $this->view->categoryList = $categoryDao->getCategoryList();
    }

    function addAction() {
        $categoryDao = new CategoryDAO();

        if ($this->_request->isPost()) {
            $name = $this->_request->getParam('name');
            $description = $this->_request->getParam('description');
            $data = array(
                'name' => $name,
                'description' => $description,
                'status' => Constant::$STATUS_AVAILABLE,
            );
            $categoryDao->addCategory($data);
            $this->_redirect('admin/category');
        }
    }

    function updateAction() {
        $categoryDao = new CategoryDAO();

        if ($this->_request->isPost()) {
            $id = $this->_request->getParam('id');
            $name = $this->_request->getParam('name');
            $description = $this->_request->getParam('description');
            $data = array(
                'id' => $id,
                'name' => $name,
                'description' => $description,
                'status' => Constant::$STATUS_AVAILABLE,
            );
            $categoryDao->editCategory($data);

            $this->_redirect('/admin/category');
        } else {
            $id = (int)$this->_request->getParam('id', 0);

            if ($id > 0) {
                $this->view->oldCategory = $categoryDao->getCategory($id);
            }
        }
    }

    function deleteAction() {
        $categoryDao = new CategoryDAO();
        $id = $this->_request->getParam('id');
        $categoryDao->deleteCategory($id);
        $this->_redirect('admin/category');
    }
}